//
//  HZLeftView.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/5/24.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZLeftView.h"

@interface HZSetModel : NSObject
@property (nonatomic, copy)NSString *model_image;
@property (nonatomic, copy)NSString *model_text;
@end
@implementation HZSetModel
@end

@interface HZLeftView()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UIImageView *pImageView;
@property (nonatomic, strong)UITableView *pTableView;
@property (nonatomic, strong)NSArray *pDataList;
@property (nonatomic, strong)UIButton *pSetBtn;
@end

@implementation HZLeftView

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kMYHexToRGB(0xf5f2ed, 1);
        [self p_commonInit];
    }
    return self;
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _pImageView = [UIImageView new];
    _pSetBtn = [UIButton new];
    _pTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _pTableView.delegate = self;
    _pTableView.dataSource = self;
    _pTableView.tableFooterView = [UIView new];
    _pTableView.backgroundColor = [UIColor clearColor];
    _pTableView.bounces = NO;
    
    _pImageView.backgroundColor = [UIColor brownColor];
    [_pSetBtn setTitle:@"设置" forState:UIControlStateNormal];
    [_pSetBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self addSubview:_pImageView];
    [self addSubview:_pTableView];
    [self addSubview:_pSetBtn];
    
    [self p_initConstraints];
    
    [self p_loadData];
}

- (void)p_initConstraints
{
    [_pImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(200);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pImageView.mas_bottom);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(_pSetBtn.mas_top);
    }];
    [_pSetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(30);
    }];
}

- (void)p_loadData
{
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:5];
    HZSetModel *model1 = [HZSetModel new];
    model1.model_text = @"我的账户";
    HZSetModel *model2 = [HZSetModel new];
    model2.model_text = @"公告信息";
    [tempArray addObject:model1];
    [tempArray addObject:model2];
    
    _pDataList = tempArray;
    [_pTableView reloadData];
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pDataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"setCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    HZSetModel *model = _pDataList[indexPath.row];
    cell.textLabel.text = model.model_text;
    cell.imageView.image = [UIImage imageNamed:model.model_image];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UIViewController *nextVC = nil;
    switch (indexPath.row) {
        case 0:
            nextVC = [[MYRouter shared] matchController:kRouterMe];
            break;
        case 1:
            nextVC = [[MYRouter shared] matchController:kRouterNotice];
            break;
        default:
            break;
    }
    
    MYRouterBlock block = [[MYRouter shared] matchBlock:kRouterBlockLeftViewList];
    block(nil, nextVC);
}

@end
